﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlowerModel;
using FlowerBLL;
using System.Data.SqlClient;

namespace FlowerManagement.Controllers
{
    public class AdminController : Controller
    {
        AdminBLL bll = new AdminBLL();
        FlowersBLL Fbll = new FlowersBLL();
        //
        // GET: /Admin/

        #region 后台管理页面
        /// <summary>
        /// 后台管理主页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 后台管理首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Welcome()
        {
            return View();
        }
        #endregion
        
        #region 管理员登录
        /// <summary>
        /// 创建后台管理登录视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 实现登录
        /// </summary>
        /// <returns></returns>
        public int LoginDo()
        {
            var name = Request["name"];
            var pwd = Request["pwd"];
            var user = bll.AdminLogin<admin>(name , pwd);

            if (Request["yzm"].ToString().ToUpper() == Session["CheckCode"].ToString().ToUpper())
            {
                if (user != null && user.Id != 0)
                {
                    Session["Name"] = user.Name;
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            return 2;
        }

        #endregion
  
        #region 客户管理
        /// <summary>
        /// 客户管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Client()
        {
            ViewBag.EmpList = bll.UsersList<Users>();
            return View();
        }

        //客户修改状态
        public int UpdateState()
        {
            var state = Convert.ToInt32(Request["state"]);
            var id = Convert.ToInt32(Request["id"]);
            if (bll.UpdateState(state , id) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 销售管理
        /// <summary>
        /// 销售订单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult SellOrder()
        {
            return View();
        }
        /// <summary>
        /// 销售订单详情
        /// </summary>
        /// <returns></returns>
        public ActionResult SellOrderDetail()
        {
            return View();
        }
        #endregion

        #region  花卉管理

        /// <summary>
        /// 花卉管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Flowers()
        {

            return View(Fbll.GetFlowers<Flowers>());
        }
        #endregion

        #region 花卉修改状态

        /// <summary>
        /// 花卉修改状态
        /// </summary>
        /// <returns></returns>
        public int UpdateSatateFlowers()
        {
            var state = Convert.ToInt32(Request["state"]);
            var id = Convert.ToInt32(Request["id"]);
            if (Fbll.UpdateStateFlowers(state, id) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        #endregion

        #region 花卉修改反填数据
        /// <summary>
        /// 花卉修改反填数据
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateBackFlowers(int id)
        {
            return View(Fbll.GetStock<Flowers>(id));
        }
        #endregion

        #region  花卉修改

        /// <summary>
        /// 花卉修改
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateFlowers(Flowers flo)
        {
            flo.AddTime = DateTime.Now;
            if (Fbll.PurchaseStock<Flowers>(flo) == true)
            {
                return Content("<script>alert('修改成功');location.href='/Admin/Flowers';</script>");
            }
            else
            {
                return Content("<script>alert('修改失败');location.href='/Admin/UpdateBackFlowers';</script>");
            }
        }

        #endregion

        #region 采购 进货

        /// <summary>
        /// 进货管理视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Stock()
        {
          
            if (Fbll.GetMoney()==0)
            {
                ViewBag.money = 0;
            }
            else
            {
                ViewBag.money = Fbll.GetMoney();
            }
  
            return View(Fbll.Getlist());
        }
        /// <summary>
        /// 实现进货
        /// </summary>
        /// <returns></returns>
        public int StockDo(Stock sto)
        {
          

            string scokid = Fbll.GetStockId().ToString();//获取数据库的订单号
            if(scokid=="")
            {
                scokid = "2017080900000";
            }
            else
            {
                scokid = Fbll.GetStockId().ToString();
            }

            sto.StockId = CreateOrderNumber(scokid);
            sto.ProviderName = Request["select"];

            SqlParameter par = new SqlParameter();
            par.ParameterName = "@StockId";
            par.Value = sto.StockId;

            string str=Request["str"];


            string flowersname = Request["flowername"];
            string num = Request["num"];

            var list = Fbll.GetFlowers<Flowers>();

            if(str=="")
            {
                return -2;
            }
            if (Fbll.AddStock1<Stock>(sto))
            {

                int stockid = Fbll.GetStockList<Stock>(par).FirstOrDefault().Id; //获取到进货表ID
                int UpdateNum = Fbll.UpdateNum(list, flowersname, num);//修改库存
                int result = Fbll.UpdateFlowers(stockid, str);       
                return result;
            }
            else
            {
                return -1;
            }          
        }

        //根据当前日期生成订单
        public static string CreateOrderNumber(string thisOrderNumber)
        {
            string num = thisOrderNumber.Substring(8,5);

            int newNum = int.Parse(num) + 1;

            return DateTime.Now.ToString("yyyyMMdd") + newNum.ToString().PadLeft(5, '0');
        }


        /// <summary>
        /// 采购列表
        /// </summary>
        /// <returns></returns>
        public ActionResult AddStock()
        {

            return View(Fbll.GetFlowers<Flowers>());
        }

        //按花的种类查询
        public ActionResult Selstock(string FlowersName)
        {

            SqlParameter par = new SqlParameter();
            par.ParameterName = "@FlowersName";
            par.Value = FlowersName;
            List<Flowers> list = Fbll.GetStockList<Flowers>(par);
            return View("AddStock", list);
        }

        //采购商品
        public int AddStockDo(int id)
        {
            FlowersMasters masters = new FlowersMasters();
            masters.Num = Convert.ToInt32(Request["num"]);

            var list = Fbll.GetStock<Flowers>(id);
            masters.FlowersName = list.FlowersName;
            masters.StockPrice = list.StockPrice;
            masters.Image = list.Image;
            masters.AddTime = DateTime.Now;
            masters.TypeId = list.TypeId;
            masters.TypeId1 = list.TypeId1;


            if (Fbll.AddStock1<FlowersMasters>(masters))
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }

        #endregion
  
        #region 实现添加商品方法（暂未使用）
        /// <summary>
        /// 实现添加商品方法
        /// </summary>
        /// <param name="flo"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public ActionResult AddStock1Do(Flowers flo, HttpPostedFileBase img)
        {
            flo.AddTime = DateTime.Now;
            if (img != null)
            {
                string imgname = flo.FlowersName + img.FileName.Substring(img.FileName.LastIndexOf('.'));

                img.SaveAs(Server.MapPath("/Files" + imgname));

                flo.Image = "/Files" + imgname;

                if (Fbll.AddStock1<Flowers>(flo) == true)
                {
                    return Content("<script>alert('添加成功！');location.href='/Admin/AddStock';</script>");
                }
                else
                {
                    return Content("<script>alert('添加失败！');location.href='/Admin/AddStock1';</script>");
                }
            }
            else
            {
                return Content("<script>alert('请选择要上传的图片');location.href='/Home/AddStock';</script>");
            }
        }
        #endregion

        #region  进货订单管理
        /// <summary>
        /// 进货订单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult StockOrder()
        {

            return View(Fbll.GetFlowers<Stock>());
        }
        #endregion
        
        #region 进货订单管理明细
        /// <summary>
        /// 进货订单管理明细
        /// </summary>
        /// <returns></returns>
        public ActionResult StockOrderDetail(int?id)
        {
            var StockId = id;
            SqlParameter par = new SqlParameter();
            par.ParameterName = "@StockId";
            par.Value = StockId;
            ViewBag.money = Fbll.GetXqMoney(StockId);
            return View(Fbll.GetStockList<FlowersMasters>(par));
        }
        #endregion
       
        #region 员工管理
        /// <summary>
        /// 员工管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Employee()
        {
            ViewBag.Emp = bll.Employ<Employee>();
            return View();
        }
        #endregion
        
        #region 添加员工信息
        /// <summary>
        /// 添加员工信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddEmploy()
        {
            ViewBag.role = bll.role<EmployeeRole>();
            return View();
        }

        public ActionResult AddEmployDo(Employee emp)
        {
            emp.RegistTime = DateTime.Now;
            if (bll.AddEmploy<Employee>(emp))
            {
                return Content("<script>alert('添加成功');location.href='/Admin/Employee';</script>");
            }
            return Content("<script>alert('添加失败！');location.href='/Admin/Employee';</script>");
        }
        #endregion 
       
        #region 修改状态
        public int UpdateEmp()
        {
            var state = Convert.ToInt32(Request["state"]);
            var id = Convert.ToInt32(Request["id"]);
            if (bll.UpdateEmp<Employee>(state , id) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 员工反填数据
        /// <summary>
        /// 员工反填数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UpdateBack(int id)
        {
            return View(Fbll.GetStock<Employee>(id));
        }
        #endregion

        #region 员工修改
        ///// <summary>
        ///// 员工修改
        ///// </summary>
        ///// <param name="emp"></param>
        ///// <returns></returns>
        public ActionResult UpdateEmploy(Employee flo)
        {
            flo.RegistTime = DateTime.Now;
            if (Fbll.PurchaseStock<Employee>(flo))
            {
                return Content("<script>alert('修改成功');location.href='/Admin/Employee';</script>");
            }
            return Content("<script>alert('修改失败！');location.href='/Admin/UpdateBack';</script>");
        }
        #endregion
        
    }
}
